package com.doudou.collection.domain.service.impl;


import com.doudou.collection.dao.mapper.BlogMapper;
import com.doudou.collection.dao.mapper.UserCollectMapper;
import com.doudou.collection.dao.mapper.UserLikeMapper;
import com.doudou.collection.domain.service.UserLikeCollectService;
import com.doudou.collection.infrastructure.common.ex.ServiceException;
import com.doudou.collection.infrastructure.common.security.CurrentPrincipal;
import com.doudou.collection.infrastructure.common.security.SecurityUtils;
import com.doudou.collection.infrastructure.common.web.StatusCode;
import com.doudou.collection.orm.dto.UserCollectDTO;
import com.doudou.collection.orm.dto.UserLikeDTO;

import com.doudou.collection.orm.entity.UserCollect;
import com.doudou.collection.orm.entity.UserLiked;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class UserLikeCollectServiceImpl implements UserLikeCollectService {

    @Autowired
    private UserLikeMapper userLikeMapper;

    @Autowired
    private UserCollectMapper userCollectMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public String userLike(UserLikeDTO userLikeDTO,Long blogId) {


        UserLiked userLiked = new UserLiked();
        CurrentPrincipal loginUser = SecurityUtils.getLoginUser();
        userLiked.setUserId(loginUser.getUserId());
        userLiked.setBlogId(blogId);
        userLiked.setCreateTime(LocalDateTime.now());
        log.debug("用户点赞数据:{}",userLiked);


        //判断用户是否登录
        if (userLiked.getUserId() == null){
            log.debug("------------------------------------{}",userLikeDTO.getUserId());
            throw new ServiceException(StatusCode.NOT_LOGIN);
        }


        int count = userLikeMapper.select(userLiked.getBlogId(),userLiked.getUserId());
        log.debug("查看用户是否已经点过赞:{}",count);

        if (count==0){
            userLikeMapper.insert(userLiked);
            log.info("点赞成功!");
            blogMapper.updateAddLikedCount(blogId);
            log.info("增加文章点赞量成功");
            return "点赞成功!";
        } else {
            userLikeMapper.delete(userLiked);
            log.debug("取消点赞成功");
            blogMapper.updateDecreaseLikedCount(blogId);
            log.info("取消文章点赞量成功");
            return "取消点赞成功!";
        }

    }

    @Override
    public String userCollect(UserCollectDTO userCollectDTO,Long blogId) {



        UserCollect userCollect = new UserCollect();
        CurrentPrincipal loginUser = SecurityUtils.getLoginUser();
        userCollect.setUserId(loginUser.getUserId());
        userCollect.setBlogId(blogId);
        userCollect.setCreateTime(LocalDateTime.now());
        log.debug("用户收藏数据:{}",userCollect);


        //判断用户是否登录
        if (userCollect.getUserId() == null){
            throw new ServiceException(StatusCode.NOT_LOGIN);
        }

        int count = userCollectMapper.select(userCollect.getBlogId(),userCollect.getUserId());
        log.debug("查看用户是否已经收藏:{}",count);

        if (count==0){
            userCollectMapper.insert(userCollect);
            log.info("收藏成功!");
            blogMapper.updateAddCollectCount(blogId);
            log.info("增加文章收藏数量成功");
            return "收藏成功!";
        } else {
            userCollectMapper.delete(userCollect);
            log.debug("取消收藏成功");
            blogMapper.updateDecreaseCollectCount(blogId);
            log.info("取消文章收藏量成功");
            return "取消收藏成功!";
        }

    }
}
